{ Statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=2784
  Verdict: Accepted
}
uses
	SysUtils, Math;
 
const
	MAXN = 510;
	INF = round(1e12);
 
type
	edge = record
		a, b, cap, flow : int64;
	end;
 
var
	i, n, m, s, t, ec, x, y, z : longint;
	e : array[0..20010] of edge;
	d, ptr, q : array[0..MAXN] of int64;
	g : array[0..MAXN] of array of int64;
 
procedure addEdge(a, b, cap : int64);
 
var
	tt : longint;
	e1, e2 : edge;
 
begin
	e1.a := a;
	e1.b := b;
	e1.cap := cap;
	e1.flow := 0;
	e2.a := b;
	e2.b := a;
	e2.cap := 0;
	e2.flow := 0;
	tt := length(g[a]);
	setlength(g[a], tt + 1);
	g[a][tt] := ec;
	e[ec] := e1;
	inc(ec);
	tt := length(g[b]);
	setlength(g[b], tt + 1);
	g[b][tt] := ec;
	e[ec] := e2;
	inc(ec);
end;
 
function bfs() : boolean;
 
var
	qh, qt, v, i, tto, id, tt : longint;
 
begin
	qh := 1;
	qt := 1;
	q[1] := s;
	fillchar(d, sizeof(d), -1);
	d[s] := 0;
	while (qh <= qt) and (d[t] = -1) do begin
		v := q[qh];
		tt := length(g[v]) - 1;
		for i := 0 to tt do begin
			id := g[v][i];
			tto := e[id].b;
			if (d[tto] = -1) and (e[id].flow < e[id].cap) then begin
				inc(qt);
				q[qt] := tto;
				d[tto] := d[v] + 1;
			end;
		end;
		inc(qh);	
	end;
	result := d[t] <> -1;
end;
 
function dfs(v, flow : int64) : int64;
 
var
	tt, id, tto, pushed : int64;
 
begin
	if flow = 0 then begin
		result := 0;
		exit;
	end;
	if v = t then begin
		result := flow;
		exit;
	end;
	tt := length(g[v]);
	while ptr[v] < tt do begin
		id := g[v][ptr[v]];
		tto := e[id].b;
		if d[tto] = d[v] + 1 then begin
			pushed := dfs(tto, min(flow, e[id].cap - e[id].flow));
			if pushed > 0 then begin
				inc(e[id].flow, pushed);
				dec(e[id xor 1].flow, pushed);
				result := pushed;
				exit;
			end;
		end;
		inc(ptr[v]);
	end;
	result := 0;
end;
 
function dinic() : int64;
 
var
	flow, pushed : int64;
 
begin
	flow := 0;
	while true do begin
		if not bfs() then break;
		fillchar(ptr, sizeof(ptr), 0);
		pushed := dfs(s, INF);
		while pushed > 0 do begin
			inc(flow, pushed);
			pushed := dfs(s, INF);
		end;
	end;
	result := flow;
end;
 
 
begin
	reset(input,'input.txt');
	rewrite(output,'output.txt');
	read(n, m);
	s := 1;
	t := n;
	ec := 0;
	for i := 1 to m do begin
		read(x, y, z);
		addEdge(x, y, z);
	end;
	write(dinic());
end.